package com.learn.base;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 结果：原子的，可见的，顺序：公平锁 、非顺序：非公平锁
 */
public class SendMoneyLock {
    private Lock lock = new ReentrantLock();
    private int mAcount = 0;
    public SendMoneyLock() {

    }
    public void sendMoney(int time,int money, String tName) {
        lock.lock();
        try {
            Thread.sleep(100);
            mAcount += money;
            System.out.println("线程："+tName+"，第【"+time+"】次存钱，账户="+mAcount);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public Integer getAccout() {
        System.out.println("Accout:"+mAcount);
        return mAcount;
    }
}
